define(['jquery', 'bootstrap', 'backend', 'table', 'form','bootstrap-datetimepicker'], function ($, undefined, Backend, Table, Form, datetimepicker) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            $(".btn-add").data("area",["1500px","90%"]);
            $(".btn-edit").data("area",["1500px","90%"]);
            Table.api.init({
                extend: {
                    index_url: 'order/sell/index' + location.search,
                    add_url: 'order/sell/add',
                    edit_url: 'order/sell/edit',
                  /*
                    del_url: 'order/sell/del',*/
                    multi_url: 'order/sell/multi',
                    import_url: 'order/sell/import',
                    table: 'order_sell',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                searchFormVisible: true,
                fixedRightNumber: 4,


                /*
                                fixedRightNumber: 1,
                */
                columns: [
                    [
                        {checkbox: true},
/*
                        {field: 'id', title: __('Id')},
*/
                        {field: 'take_number', title: __('Take_number'), operate: 'LIKE'},
                        {field: 'selltime', title: __('Selltime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'buy_customer', title: __('买箱客户'), operate: 'LIKE'},
                        {field: 'sell_number', title: __('Sell_number'), operate: false},
                        {field: 'complete_number', title: __('完成数量'), operate: false},
                        {field: 'type.name', title: __('Bin_type'), searchList: $.getJSON('bin.type/searchlist')},
                        {field: 'native.name', title: __('Bin_native'), searchList: $.getJSON('bin.native/searchlist')},
                        {field: 'takeyard.organ_name', title: __('Take_yard_id'), operate: false},
                        {field: 'admin.nickname', title: __('Salesman_id'), operate: 'LIKE'},
                        {field: 'unit_price', title: __('Unit_price'), operate:'BETWEEN'},
                        {field: 'currency_type', title: __('Currency_type'), searchList: {"1":__('Currency_type 1'),"2":__('Currency_type 2'),"3":__('Currency_type 3'),"4":__('Currency_type 4')}, formatter: Table.api.formatter.normal},
/*
                        {field: 'weigh', title: __('Weigh'), operate: false},
*/
                        {field: 'is_finish', title: __('Is_finish'), searchList: {"0":__('Is_finish 0'),"1":__('Is_finish 1')}, formatter: Table.api.formatter.normal},

                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'sell_sn', title: __('Sell_sn'), operate: 'LIKE'},
                        {field: 'bin_origin', title: __('Bin_origin'), operate: false, searchList: {"1":__('Bin_origin 1'),"2":__('Bin_origin 2')}, formatter: Table.api.formatter.normal},
                        {field: 'last_remark', title: __('最后备注'),  align: 'left',operate: false},


                        /*
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
*/
/*
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
*/
                        {
                            field: 'remark',
                            width: "100px",
                            title: '备注',
                            table: table,
                            formatter: Table.api.formatter.buttons,
                            buttons: [

                                {
                                    name: 'addtabs',
                                    title: __('查看全部备注'),
                                    text:  function(row,value){

                                        return '备注('+row.remark_total_nuber+')';
                                        /*
                                                                                return '备注';
                                        */


                                    },
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: function(row,value){
                                        return "remark/remark_list/correspond_id/"+row.id+'/type/2';
                                    }
                                },
                            ]
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('箱子列表'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: function(row,value){

                                        return '查看('+row.complete_number+')';


                                    },
                                    extend:'data-area=\'["900px","100%"]\' data-shade=\'[0.5,"#000"]\'',
                                    title: '箱子列表',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: function(row,value){
                                        // 假设URL是 http://example.com/page?name=John&age=30

                                        return "bin.sell/index/sellorder.sell_sn/"+row.sell_sn;
                                    },
                                    hidden:function (row){
                                        /*  if(row.current_status==2){
                                              return true;
                                          }*/
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('去选箱'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: '添加箱子',
                                    extend:'data-area=\'["1500px","90%"]\' data-shade=\'[0.5,"#000"]\'',
                                    title: '去选箱',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-plus',
                                    url:function (row,value){
                                        return   "order.sell/add_bin/ids/"+row.id;
                                    },
                                    hidden:function (row){
                                        if(row.is_finish==1){
                                            return true;
                                        }
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('已完成'),
                                    title: __('已完成'),
                                    classname: 'btn btn-xs btn-danger',

                                    hidden: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.is_finish!=1){
                                            return true;
                                        }

                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table',function(){
                $(".btn-editone").data("area",["1500px","90%"]);
            });
        },
        add: function () {
            $("#c-take_yard_id").focus(function(){
                var bin_type= $("#c-bin_type").val();
                var bin_native= $("#c-bin_native").val();




            });

            $("#c-choose_bin").focus(function(){
                var take_yard_id= $("#c-take_yard_id").val();
                if(!take_yard_id){
                    Toastr.error('请先选择提箱堆场!');
                    return false;
                }

                var take_number= $("#c-take_number").val();
                if(!take_number){
                    Toastr.error('请先填写提箱单号!');
                    return false;
                }


            });

            $("#c-choose_bin").data("eSelect", function(data){
                //后续操作
                var binData=$( "#c-choose_bin").val();
                var aArray = binData.split(',');
                var position = 0;
                console.log(data);
                console.log(aArray);
                for (var i = 0; i < aArray.length; i++) {
                    if(data.id==parseInt(aArray[i])){
                        position=i;
                    }
                }
                console.log(position);

                $('.btn-append').trigger("click");
                $('.bin_id:eq('+position+')').val(data.id);
                $('.bin_number:eq('+position+')').val(data.bin_sn);
                $('.bin_native:eq('+position+')').val(data.bin_native);
                $('.bin_type:eq('+position+')').val(data.bin_type);
                $('.take_yard_id:eq('+position+')').val($("#c-take_yard_id").val());

                $('.take_number').val($("#c-take_number").val());
                $('.take_null_time').val($("#c-selltime").val());

                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker({
                    format:'YYYY-MM-DD HH:mm:ss',
                });

            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        add_bin: function (){

/*
            $("#c-choose_bin").data("eSelect", function(data){

                console.log(data);
                //后续操作
                var binData=$( "#c-choose_bin").val();
                var bin=  $('#c-choose_bin').selectPageText();


                var bin_type = $("#c-bin_type").val();
                var bin_native = data.bin_native;
                var take_number = $("#c-take_number").val();
                var take_yard_id = $("#c-take_yard_id").val();
/!*
                var selltime = $("#c-selltime").val();
*!/


                $('.btn-remove').trigger("click");


                // 将字符串分割成数组
                var aArray = binData.split(',');
                var bArray = bin.split(',');
             /!*   console.log(aArray);
                console.log(bArray);
*!/

                // 创建一个二维数组，并将aArray和bArray的元素组合起来
                var combined = [];
                for (var i = 0; i < aArray.length; i++) {
                    combined.push([parseInt(aArray[i]), bArray[i]]);
                }
/!*
                console.log(combined);
*!/


                for (var j = 0; j < bArray.length; j++) {
                    $('.btn-append').trigger("click");
                    $('.bin_id:eq('+j+')').val(aArray[j]);
                    $('.bin_number:eq('+j+')').val(bArray[j]);

/!*
                    $('.take_null_time').val(selltime);
*!/
                    $('.take_number').val(take_number);
                    $('.datetimepicker').parent().css('position', 'relative');
                    $('.datetimepicker').datetimepicker({
                        format:'YYYY-MM-DD HH:mm:ss',
                    });
                    $('.bin_native:eq('+j+')').val(bin_native);
                    $('.bin_type:eq('+j+')').val(bin_type);
                    $('.take_yard_id:eq('+j+')').val(take_yard_id);
                }



            });
*/

            $("#c-choose_bin").data("eSelect", function(data){

                var binData=$( "#c-choose_bin").val();
                var aArray = binData.split(',');
                var position = 0;
                console.log(data);
                console.log(aArray);
                for (var i = 0; i < aArray.length; i++) {
                    if(data.id==parseInt(aArray[i])){
                        position=i;
                     }
                        }
                console.log(position);

                $('.btn-append').trigger("click");
                $('.bin_id:eq('+position+')').val(data.id);
                $('.bin_number:eq('+position+')').val(data.bin_sn);
                $('.bin_native:eq('+position+')').val(data.bin_native);
                $('.bin_type:eq('+position+')').val(data.bin_type);
                $('.take_yard_id:eq('+position+')').val($("#c-take_yard_id").val());

                $('.take_number').val($("#c-take_number").val());
                $('.datetimepicker').parent().css('position', 'relative');
                $('.datetimepicker').datetimepicker({
                    format:'YYYY-MM-DD HH:mm:ss',
                });

            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-bin_type", function(){

                    $('#c-take_yard_id').selectPageClear();
                });

                $(document).on("change", "#c-bin_origin", function(){

                    $('#c-take_yard_id').selectPageClear();
                });

                $(document).on("change", "#c-bin_native", function(){

                    $('#c-take_yard_id').selectPageClear();
                });


                $('#c-take_yard_id').data("params",function (obj){
                    return {custom: {bin_type: $("#c-bin_type").val(),
                            organ_type:1,bin_origin:$("#c-bin_origin").val(),
                            bin_native:$("#c-bin_native").val()
                        }};
                });

                $(document).on("change", "#c-take_yard_id", function(){

                    $('#c-choose_bin').selectPageClear();
                });
                $('#c-choose_bin').data("params",function (obj){
                    return {custom: {bin_type: $("#c-bin_type").val(),
                            organ_type:1,bin_origin:$("#c-bin_origin").val(),
                            bin_native:$("#c-bin_native").val(),
                            take_yard_id:$("#c-take_yard_id").val(),
                        }};
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
